<template>
	<view class="home h100">
		<u-navbar class="nav" :title="content" leftIcon="home" leftIconColor="#fff" leftIconSize="24px"
			:bgColor="subColor" titleStyle="color:#fff;font-size:36rpx;" :placeholder="true">
			<view class="f-c u-nav-slot" slot="left" @click="toShop">
				<view class="wei6 f18 c0">{{store.name}}</view>
				<u-icon size="18" color="#000" name="arrow-right" style="line-height: 56rpx;"></u-icon>
			</view>
			<view class="switch" v-if="adminInfo.store_id != 0" slot="left">
				<span style="margin-right: 5rpx;">上下线</span>
				<u-switch v-model="checked" :loading="loading" @change="switchBtn" size="23"
					activeColor="#7fafff"></u-switch>
			</view>
			<view class="u-nav-right" slot="right">
				<u-icon size="28" color="#000" name="scan" @click="smlogin"></u-icon>
			</view>
		</u-navbar>
		<!-- 页面内容 -->
		<view class="top c0" :style="{background:subColor}">
			<view class="picker">
				<uni-datetime-picker v-model="verify_inquire" type="daterange" @change="maskClick"
					:clear-icon="false" />
			</view>
			<view class="f-c-ac mb10">
				<view class="f-c-b">
					<text class="f12 c0 mb10 mr5">支付金额（元）</text>
					<!-- <u-icon size="17" color="#000" name="info-circle" style="line-height: 56rpx;"></u-icon> -->
				</view>

				<view class="f18">
					<text class="f26 wei5">
						<text>
							<tetxt class="f18 nowei">￥</tetxt>{{toDay || '0.00'}}
						</text>
						<!-- <text v-else>***</text> -->
					</text>
				</view>
			</view>
			<u-notify message="Hi uView" :show="true"></u-notify>
			<view class="more" :style="{height:(more_height+'rpx')}">
				<u-grid :border="false" :col="3" @click="handelClick">
					<u-grid-item class="mb10" v-for="(item,index) in baseList" :key="index">
						<text class="grid-text f12 c0 mb10">{{item.title}}</text>
						<text class="fc f16 pt5 c0 wei6">{{item.num}}</text>
						<!-- <text class="fc f16 pt5 c0 wei6" v-else>***</text> -->
					</u-grid-item>
				</u-grid>
			</view>
			<!-- 	<view class="f-c-b" @click="more">
				<text class="mr5 ce">{{more_text}}</text>
				<u-icon size="18" color="#eee" :name="more_name" style="line-height: 56rpx;"></u-icon>
			</view> -->
		</view>
		<view class="f-bt ksfix bf p2 bs10">
			<view class="f-g-1 f-c-c" @click="goUrl('/pages/home/goods/index')">
				<view>
					<image class="iconwh1" :src='`${imgPath}icon/gwc.png`' mode="aspectFit"></image>
				</view>
				<view class="f12">商品管理</view>
			</view>
			<view class="f-g-1 f-c-c" @click="goUrl('/pages/home/storeList/storeList')">
				<view>
					<image class="iconwh1" :src='`${imgPath}icon/yhq.png`' mode="aspectFit"></image>
				</view>
				<view class="f12">门店管理</view>
			</view>
			<view class="f-g-1 f-c-c" @click="goUrl('/pages/home/finance/finance')">
				<view>
					<image class="iconwh1" :src='`${imgPath}icon/hb.png`' mode="aspectFit"></image>
				</view>
				<view class="f12">财务管理</view>
			</view>
		</view>
		<!-- <view class="f-x-bt bf mb10 p-10-13 mt10" @click="toMsg">
			<view class="flex f-y-c">
				<image :src='`${imgPath}imgs/my/1.png`' style="width: 30rpx;height:30rpx"></image>
				<text class="pl5 c3" style="line-height: 48rpx;">消息中心</text>
			</view>
			<u-icon size="16" color="#999" name="arrow-right" style="line-height: 56rpx;"></u-icon>
		</view> -->
		<view class="use bf c3 p2 mt10">
			<view class="pt10">
				<swiper class="swiper" :indicator-dots="false" indicator-color="#f5f5f5"
					indicator-active-color="#d8d8d8">
					<swiper-item>
						<u-grid :border="false" col=3>
							<u-grid-item @click="toUrl(item,index)" v-if="menus.includes(item.role)"
								v-for="(item, index) in commonList" :key="index">
								<uni-icons :type="item.icon" size="25" :color="subColor"></uni-icons>
								<text class="grid-text f12">{{item.title}}</text>
							</u-grid-item>
						</u-grid>
					</swiper-item>
				</swiper>
			</view>
		</view>
		<u-modal :show="showversion" show-cancel-button @cancel="cancelTui" @confirm="confirmTui">
			<view class="slot-content" style="display: flex;align-items: center;width: 100%;">
				<view style="width: 100%;">
					检测到App更新，是否升级！
				</view>
			</view>
		</u-modal>
		<u-modal :show="showDown" :show-confirm-button="false">
			<view class="slot-content" style="display: flex;align-items: center;width: 100%;">
				<view style="width: 100%;">
					<text>下载中</text>
					<progress :percent="progress" :stroke-width="10" />
				</view>
			</view>
		</u-modal>
		<!-- <button @tap="asasasas">asasas</button> -->
		<tab-bar style="min-height: 114rpx; height: 114rpx; max-height: 114rpx;"></tab-bar>
		<button type="default" open-type="contact" class="weixinButton">
			<image src="https://faquniapp.oss-cn-hangzhou.aliyuncs.com/kefutubiao.png" class="img"></image>
			<view class="text">客服</view>
		</button>
		<!-- <u-popup :show="show" @close="closeprop" @open="openprop" mode="center" :round="20" :safeAreaInsetBottom="false"
			:closeable="true">
			<view class="propContainer">
				<img src="https://oss.kdsh.cc/storage/default/20250122/fangjiabbbbbbbb181b19269f2ef34329968ca3b1f15e165def4c37.jpg"
					class="img" />
			</view>
		</u-popup>
		<u-popup :show="show2" @close="closeprop2" @open="openprop2" mode="center" :round="20"
			:safeAreaInsetBottom="false" :closeable="true">
			<view class="propContainer2">
				<img src="https://oss.kdsh.cc//storage/default/20250120/025-01-20_15065ade3f141d631d4b42d7050b682c7d17e2a780ac5.png"
					class="img" />
			</view>
		</u-popup> -->
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import TabBar from '@/components/common/tabbar.vue'
	import site from '@/custom/siteroot.js';
	import {
		downloadApp,
		installApp
	} from '@/common/utils.js';
	export default {
		components: {
			TabBar
		},
		data() {
			return {
				show: true,
				show2: false,
				popupCount: 0,
				lastPopupDate: null,
				verify_inquire: '',
				showversion: false,
				progress: 0,
				isDownloadFinish: false,
				showDown: false,
				subColor: uni.getStorageSync('subject_color'),
				content: '',
				shadowStyle: {
					backgroundImage: "none",
					paddingTop: "0",
					marginTop: "20rpx"
				},
				adminInfo: {},
				loading: false,
				checked: false,
				more_height: 160,
				more_text: '展开更多',
				more_name: 'arrow-down',
				toDay: '',
				baseList: [{
						title: '退款金额（元）',
						num: '0.00',
					},
					{
						title: '支付单量（单）',
						num: '0',
					},
					{
						title: '退款单量（单)',
						num: '0',
					},
					// {
					// 	title: '储值金额',
					// 	num: '0.00',
					// },
					// {
					// 	title: '退款金额',
					// 	num: '0.00',
					// },
					// {
					// 	title: '会员注册',
					// 	num: '0',
					// },
				],
				imgPath: getApp().globalData.onImgurl,
				commonList: [{
						title: '连锁门店',
						icon: 'shop-filled',
						url: '/pages/mine/chain/chain',
						role: 'douyin/chain',
					},
					{
						title: '服务商',
						icon: 'person-filled',
						url: '/pages/home/agent/agent',
						role: 'douyin/agent',
					},
					{
						title: '门店审核',
						icon: 'loop',
						url: '/pages/home/shenhe/shenhe',
						role: 'douyin/shenhe',
					},
					{
						title: '商品审核',
						icon: 'cart-filled',
						url: '/pages/home/productreview/productreview',
						role: 'dy/productreview',
					},
					{
						title: '合伙人',
						icon: 'staff-filled',
						url: '/pages/home/partner/partner',
						role: 'douyin/partner',
					},
					{
						title: '上下架管理',
						icon: 'minus-filled',
						url: '/pages/home/produceSx/produceSx',
						role: 'douyin/produceSx',
					},
					{
						title: '点单管理',
						icon: 'calendar-filled',
						url: '/pages/home/diandanbox/diandanbox',
						role: 'dian',
					},
					// {
					// 	title: '融合版管理',
					// 	icon: 'bars',
					// 	url: '/pages/home/fusionbox/fusionbox',
					// 	role: 'douyin/rong',
					// },
					// {
					// 	title: '权限管理',
					// 	icon: 'bars',
					// 	url: '/pages/home/fusionbox/fusionbox',
					// 	role: 'douyin/rong',
					// },
				],
				audioContext: {
					url: ''
				}
			}
		},
		computed: {
			...mapState({
				store: state => state.store,
				storeInfo: state => state.storeInfo,
				role: state => state.user.roleData || [],
				menus: state => state.menus,
			}),
		},
		onShow() {
			this.getStoreStaus()
		},
		onLoad() {
			this.fetchData()
			let adminInfo = uni.getStorageSync('adminInfo');
			this.adminInfo = adminInfo
			if (!adminInfo) {
				uni.reLaunch({
					url: '/pages/login/index'
				})
				return
			}
			// #ifdef MP-WEIXIN
			this.setSiteroot(site.siteroot)
			// #endif
			//检测是否需要更新
			// #ifdef APP-VUE
			this.getVesion()
			// #endif

			// let token = uni.getStorageSync('token'),
			// 	storeId = uni.getStorageSync('storeId'),
			// 	uniacid = uni.getStorageSync('uniacid');
			// if (token && !storeId && !uniacid) {
			// 	uni.reLaunch({
			// 		url: '/pages/login/selectStore'
			// 	})
			// } else if (token && !storeId && uniacid) {
			// 	uni.reLaunch({
			// 		url: '/pages/login/selectShop'
			// 	})
			// } else if (token && storeId && uniacid) {
			// 	uni.switchTab({
			// 		url: '/pages/index/index'
			// 	})
			// } else {
			// 	uni.reLaunch({
			// 		url: '/pages/login/index'
			// 	})
			// }
			// if (token && storeId && uniacid) {
			// 	this.fetchData()
			// 	this.getSetInfo()
			// }
			uni.setStorageSync('subject_color', '#FDDA34')
			this.subColor = '#FDDA34'
			this.lastPopupDate = uni.getStorageSync('lastPopupDate');
			const currentDate = new Date().toISOString().split('T')[0];
			// if (this.popupCount < 3) {
			// 	if (!this.lastPopupDate || this.lastPopupDate < '2025-01-20' || (currentDate > this.lastPopupDate && this
			// 			.popupCount < 3)) {
			// 		this.show = true;
			// 	}
			// }
		},
		methods: {
			asasasas(){
				uni.navigateTo({
					url:"/pages/mine/gongzhonghao/gongzhonghao"
				})
			},
			getStoreStaus() {
				let adminInfo = uni.getStorageSync("adminInfo")
				if (adminInfo.store_id != 0) {
					this.beg.request({
						url: '/api/store_uni_app.Index/get_store_dy_status',
						method: 'post',
						data: {
							id: adminInfo.store_id
						}
					}).then(res => {
						if (res.code == 1) {
							this.checked = res.data.dy_status == 1 ? true : false
						}
					})
				}
			},
			openprop() {
				this.show = true
			},
			closeprop() {
				this.show = false;
				// 存储当前日期
				const currentDate = new Date().toISOString().split('T')[0];
				this.lastPopupDate = currentDate;
				uni.setStorageSync('lastPopupDate', this.lastPopupDate);
				this.popupCount++;
				if (this.popupCount < 3) {
					this.openprop2();
				}
			},
			openprop2() {
				this.show2 = true
			},
			closeprop2() {
				this.show2 = false;
				// 存储当前日期
				const currentDate = new Date().toISOString().split('T')[0];
				this.lastPopupDate = currentDate;
				uni.setStorageSync('lastPopupDate', this.lastPopupDate);
				this.popupCount++;
			},
			switchBtn(e) {
				this.loading = true
				let userinfo = {
					id: this.adminInfo.store_id
				}
				if (e) {
					userinfo.dy_status = 1

				} else {
					userinfo.dy_status = 2
				}
				this.beg.request({
					url: '/admin/store.Store/setOpStatus',
					method: 'post',
					data: userinfo
				}).then(res => {
					this.loading = false
					if (res.code == 1) {
						uni.$u.toast(res.msg)
						this.updateStoreDy()
					}
				})
			},
			updateStoreDy() {
				this.beg.request({
					url: '/admin/store.Store/dy_store_sync',
					method: 'post',
					data: {
						id: this.adminInfo.store_id
					}
				}).then(res => {
					if (res.code == 1) {} else {
						uni.$u.toast('操作失败')
					}
				})
			},
			maskClick(e) {
				this.verify_inquire = e
				this.fetchData()
			},
			async getVesion() {
				let {
					code,
					data,
					msg,
				} = await this.beg.request({
					url: '/index.php/api/store_uni_app.Index/getAppVersion',
					method: 'post',
					data: {}
				});
				console.log(data)
				if (code == 1) {
					//取域
					let version = uni.getStorageSync('version');
					console.log(version);
					if (version) {
						if (version != data.version) {
							//需要升级
							this.showversion = true;
							this.tempFilePath = data.url;
						}
					} else {
						uni.setStorageSync('version', data.version);
					}
				}
			},
			cancelTui() {
				//取消升级
				this.showversion = false;
			},
			confirmTui() {
				let that = this;
				//确定升级
				uni.downloadFile({
					url: '',
					success(res) {
						that.showversion = false;
						that.showDown = true;
						downloadApp(that.tempFilePath, current => {
							//下载进度
							that.progress = current
						}).then(fileName => {
							//下载完成
							that.isDownloadFinish = true
							if (that.isDownloadFinish) {
								//自动安装App
								that.handleInstallApp(fileName)
							}
						}).catch(e => {
							console.log(e, 'e')
						})
					},
					fail() {
						cancelTui();
						console.log('获取失败')
					}
				})
			},
			//安装app
			handleInstallApp(fileName) {
				//下载完成才能安装，防止下载过程中点击
				if (this.isDownloadFinish) {
					installApp(fileName, () => {
						//安装成功,关闭升级弹窗
						this.showDown = false;
					})
				}
			},
			...mapMutations(["setStoreId"]),
			...mapMutations(["setSiteroot"]),
			async fetchData() {
				uni.showLoading()
				await this.beg.request({
					url: '/admin/Index/statistics',
					data: {
						verify_inquire: this.verify_inquire
					},
					method: 'post'
				}).then(res => {
					uni.hideLoading()
					this.toDay = parseFloat(res.data.total_amount_success / 100).toFixed(2)
					this.baseList[0].num = parseFloat(res.data.total_amount_fail / 100).toFixed(2)
					this.baseList[1].num = parseFloat(res.data.order_success)
					this.baseList[2].num = parseFloat(res.data.return_count)
				})
			},
			leftClick() {
				console.log('leftClick');
			},
			load() {
				this.$refs.uReadMore.init();
			},
			toShop() {
				// this.setStoreId('')
				// uni.reLaunch({
				// 	url: '/pages/login/selectShop'
				// })
			},
			toGroup() {
				console.log('000');
			},
			toMsg() {
				console.log('消息中心');
			},
			more() {
				if (this.more_height === 160) {
					this.more_height = 320
					this.more_text = '收起'
					this.more_name = 'arrow-up'
				} else {
					this.more_height = 160
					this.more_text = '展开更多'
					this.more_name = 'arrow-down'
				}
			},
			toUrl(item, index) {
				if (item.value == 'sys') {
					var that = this
					uni.scanCode({
						onlyFromCamera: true,
						success: function(res) {
							if (res.result) {
								that.hexiao(res.result)
							}
						},
					});
				} else if (item.value == 'gdgg' || item.value == 'ddcx') {
					uni.switchTab({
						url: item.url
					})
				} else if (item.url != '') {
					uni.navigateTo({
						url: item.url
					});
				}

			},
			async hexiao(i) {
				let {
					data,
					msg,
				} = await this.beg.request({
					url: `${this.api.verification}/${i}`,
					method: 'POST',
				})
				uni.$u.toast(msg)
			},
			async getSetInfo() {
				let {
					data
				} = await this.beg.request({
					url: this.api.systemConfig
				})
				uni.setStorageSync('setInfo', data)
			},
			handelClick() {

			},
			goUrl(url) {

				uni.navigateTo({
					url,
				});

			},
			async smlogin(i) {
				const that = this
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						console.log('res', res)
						if (res.result) {
							let r = JSON.parse(decodeURIComponent(res.result))
							that.qrlogin(r)
						}
					},
				})
			},
			async qrlogin(i) {
				uni.showModal({
					title: '提示',
					content: '确认是否登录吗?',
					success: async (res) => {
						if (res.confirm) {
							let {
								data,
								msg,
							} = await this.beg.request({
								url: this.api.qrLogin,
								method: 'POST',
								data: {
									requestId: i.requestId,
									data: {
										type: i.type,
										uniacIf: uni.getStorageSync('storeInfo'),
										storeIf: uni.getStorageSync('store'),
										token: uni.getStorageSync('token'),
									}
								}
							})
							uni.$u.toast(msg)
						}
					}
				})
			},
		},
		async onPullDownRefresh() {
			// await this.fetchData()
			uni.stopPullDownRefresh();
		},
		onShareAppMessage() {
			return {
				title: this.store.name,
				imageUrl: this.storeInfo.applyImage,
				path: '/pages/index/index',
			}
		},
		onShareTimeline(e) {
			return {
				title: this.store.name,
				imageUrl: this.storeInfo.applyImage,
			}
		},
	}
</script>

<style lang="scss" scoped>
	.home {
		overflow: auto;
		background-color: #f8f8f8;
		padding-bottom: 120rpx;

		.nav {
			position: relative;

			.iconfont {
				padding-left: 10rpx;
				font-size: 38rpx;
				color: #fff;
			}
		}

		.top {
			padding: 40rpx 0 80rpx 0;
			width: 100%;

			.more {
				overflow: hidden;
			}

			/deep/.u-grid {
				.u-grid-item {
					height: 156rpx;
					padding: 20rpx;
				}
			}
		}

		.use {
			.iconfont {
				font-size: 60rpx;
			}

			/deep/.u-grid-item {
				padding: 20rpx 20rpx 20rpx 20rpx;
				height: 140rpx;
				justify-content: space-between;
				align-items: center !important;
			}

			/deep/.u-grid .u-border-bottom,
			.u-border-right {
				border-color: #eee !important;
			}
		}

		.propContainer {
			height: 70vh;

			.img {
				height: 70vh;
				border-radius: 20rpx;
			}

		}

		.propContainer2 {
			height: 70vh;

			.img {
				height: 70vh;
				border-radius: 20rpx;
			}
		}
	}

	.iconwh {
		width: 50rpx;
		height: 50rpx;
	}

	.iconwh1 {
		width: 60rpx;
		height: 60rpx;
	}

	.ksfix {
		z-index: 20;
		margin: 0 30rpx;
		margin-top: -60rpx;
		height: 150rpx;
	}

	.u-nav-right {
		// #ifdef MP-WEIXIN
		padding-right: 100px;
		// #endif
	}

	.picker {
		position: absolute;
		right: 0;

		::v-deep .uni-icons {
			font-size: 70rpx !important;
			color: #711212 !important;
		}
	}

	.switch {
		display: flex;
		align-items: center;
		margin-left: 50rpx;
	}

	::v-deep .uni-date-x {
		background-color: #FDDA34;
	}

	::v-deep .uni-calendar--fixed {
		bottom: calc(57px);
	}

	::v-deep .uni-date {
		width: 1%;
	}

	::v-deep .uni-date__x-input {
		display: none;
	}

	::v-deep .range-separator {
		display: none;
	}

	::v-deep .uni-date-x--border {
		border: 0;
	}

	.weixinButton {
		width: 104rpx;
		height: 104rpx;
		border-radius: 50%;
		background: #b6c443e8;
		box-shadow: 0 4rpx 20rpx 0 rgba(0, 0, 0, 0.1);
		position: fixed;
		right: 32rpx;
		bottom: 20vh;
		display: flex;
		justify-content: center;
		flex-direction: column;

		.img {
			width: 52rpx;
			height: 52rpx;
		}

		.text {
			width: 48rpx;
			height: 34rpx;
			font-size: 24rpx;
			font-weight: 500;
			color: rgba(255, 255, 255, 1);
			margin-left: 4rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}

	.swiper {
		height: 495rpx;
	}

	.grid-text {
		font-size: 14px;
		padding: 10rpx 0 20rpx 0rpx;
	}
</style>